﻿using System;
using System.IO;
using System.Web.Configuration;
using SlamCms.Common;

namespace SlamCms.Configuration
{
	internal static class ConfigurationHelpers
	{
		public static T GetEnvironmentSetting<T>(string key, T defaultValue)// where T : struct
		{
			try
			{
				if (ConfigurationManager.Environment.Settings.ContainsKey(key))
					return ConfigurationManager.Environment.Settings.Get(key, defaultValue);
				
				var stringValue = WebConfigurationManager.AppSettings[key];
				if (!stringValue.IsNullOrWhiteSpace())
					return (T)Convert.ChangeType(stringValue, typeof(T));
				
			}
			catch (FileNotFoundException)
			{
			}
			catch (DirectoryNotFoundException)
			{
			}
			catch (InvalidCastException)
			{
			}

			return defaultValue;
		}
	}
}
